*Inaction rates for estabs inactive at every tau
cd /OEUS/users/FEDSusers/JOLTS/
set more off

use ./data/JOLTS_ntilde, clear

gen flag = 1 if emp>0 & emp!=.
foreach i of numlist 1/24{
	replace flag = 0 if emp==.|ntilde`i'==.|emp==0|ntilde`i'==0
	gen inactalt_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >0  
	replace inactalt_`i' = 1 if emp == ntilde`i'  & inactalt_`i' == 0 & flag==1
	replace flag = 0 if inactalt_`i'==0
}


*Define quarterly inaction with a window as maximum of emp and percentage
*max(1,1%)
gen flag11 = 1 if emp>0 & emp!=.
foreach i of numlist 1/24{
	replace flag11 = 0 if emp==.|ntilde`i'==.|emp==0|ntilde`i'==0
	gen inactalt11_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >0 
	replace inactalt11_`i' = 1 if emp >= min(floor(.99*ntilde`i'),ntilde`i'-1) & emp<=max(ceil(1.01*ntilde`i'),ntilde`i'+1) & inactalt11_`i' == 0 & flag11==1
	replace flag11 = 0 if inactalt11_`i'==0
	 
}

*max(2,2%)
gen flag22 = 1 if emp>0 & emp!=.
foreach i of numlist 1/24{
	replace flag22 = 0 if emp==.|ntilde`i'==.|emp==0|ntilde`i'==0
	gen inactalt22_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >0 
	replace inactalt22_`i' = 1 if emp >= min(floor(.98*ntilde`i'),ntilde`i'-2) & emp<=max(ceil(1.02*ntilde`i'),ntilde`i'+2) & inactalt22_`i' == 0 & flag22==1
	replace flag22 = 0 if inactalt22_`i'==0
	
}
cd /OEUS/users/JOLTS/rnd2final/

*********Estab Weight
preserve
collapse (mean) inactalt_* inactalt11_* inactalt22_* (count) N = weight_f [aw=weight_f]
gen month=0
reshape long inactalt_ inactalt11_ inactalt22_, i(month)
drop month
rename _j month
save inactdecay_everytau_estab_rnd2final, replace
outsheet using inactdecay_everytau_estab_rnd2final.csv, c replace
restore

**********Emp Weight
preserve
gen weight2 = weight_f*emp
collapse (mean) inactalt_* inactalt11_* inactalt22_* (count) N = weight_f [aw=weight2]
gen month=0
reshape long inactalt_ inactalt11_ inactalt22_, i(month)
drop month
rename _j month
save inactdecay_everytau_emp_rnd2final, replace
outsheet using inactdecay_everytau_emp_rnd2final.csv, c replace
restore
*********Cond. on emp>20, estab weight
preserve
collapse (mean) inactalt_* inactalt11_* inactalt22_* (count) N = weight_f if emp>20 [aw=weight_f]
gen month=0
reshape long inactalt_ inactalt11_ inactalt22_, i(month)
drop month
rename _j month
save inactdecay_everytau_g20_estab_rnd2final, replace
outsheet using inactdecay_everytau_g20_estab_rnd2final.csv, c replace
restore

*********Cond. on emp>20, emp weight
preserve
gen weight2 = weight_f*emp
collapse (mean) inactalt_* inactalt11_* inactalt22_* (count) N = weight_f if emp>20 [aw=weight2]
gen month=0
reshape long inactalt_ inactalt11_ inactalt22_, i(month)
drop month
rename _j month
save inactdecay_everytau_g20_emp_rnd2final, replace
outsheet using inactdecay_everytau_g20_emp_rnd2final.csv, c replace
restore

